import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.swing.*;


public class PhysicsLab {
   public static void main(String[] args) throws FileNotFoundException, IOException {
      Properties props = new Properties();
      FileInputStream fichero = new FileInputStream("PhysicsLab.ini");
      props.load(fichero);
      MyWorld world = new MyWorld();
      if(!props.getProperty("delta_time").isEmpty())
      world.setDelta_t(Float.parseFloat(props.getProperty("delta_time")));
      if(!props.getProperty("refresh_time").isEmpty());
      world.setRefreshPeriod(Float.parseFloat(props.getProperty("refresh_time")));
      PhysicsLabFrame labFrame = new PhysicsLabFrame(world);
      labFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      labFrame.setVisible(true);
   }
}

class PhysicsLabFrame extends JFrame {
       public PhysicsLabFrame(MyWorld w) {
      setTitle("Small Physics Laboratory");
      setSize(MyWorld.WIDTH*2, MyWorld.HEIGHT+50);
      MyWorld world = w;
      PhysicsLabPanel labPanel = new PhysicsLabPanel(world);
      graphsPanel pan = new graphsPanel();
      JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                           labPanel,pan);
      splitPane.setOneTouchExpandable(false);
      splitPane.setDividerSize(1);
      splitPane.setDividerLocation(MyWorld.WIDTH);
      add(splitPane);  // it is equivalent to getContentPane().add(...);
      world.setPanel(labPanel);   // to repaint view when model changes
      world.setgPanel(pan);
      ConfigurationMenuListener cListener = new ConfigurationMenuListener(world);
      WorldControlMenuListener wcListener = new WorldControlMenuListener(world);
      ActivateMenuListener amListener = new ActivateMenuListener();
      setJMenuBar(createLabMenuBar(cListener, wcListener,amListener));
   }


   public JMenuBar createLabMenuBar(ConfigurationMenuListener cl, WorldControlMenuListener wl,ActivateMenuListener am) {
      JMenuBar mb = new JMenuBar();
      JMenu menu = new JMenu ("Configuration");
      am.get_menu(menu);
      mb.add(menu);
      JMenu subMenu = new JMenu("Insert");  
      menu.add(subMenu);
      JMenuItem menuItem = new JMenuItem("Delete");
      menuItem.addActionListener(cl);
      menu.add(menuItem);
      menuItem = new JMenuItem("Select Ball");
      menuItem.addActionListener(cl);
      menu.add(menuItem);
      menuItem = new JMenuItem("Set attribute...");
      menuItem.addActionListener(cl);
      menu.add(menuItem);
      
      menuItem = new JMenuItem("Ball");
      menuItem.addActionListener(cl);
      subMenu.add(menuItem);
      menuItem = new JMenuItem("Fixed Hook");
      menuItem.addActionListener(cl);
      subMenu.add(menuItem);
      menuItem = new JMenuItem("Rubber");
      menuItem.addActionListener(cl);
      subMenu.add(menuItem);
      menuItem = new JMenuItem("Spring");
      menuItem.addActionListener(cl);
      subMenu.add(menuItem);
      
      menu = new JMenu("MyWorld");
      mb.add(menu);
      menuItem = new JMenuItem("Start");
      menuItem.addActionListener(wl);
      menuItem.addActionListener(am);
      menu.add(menuItem);
      menuItem = new JMenuItem("Resume");
      menuItem.addActionListener(wl);
      menuItem.addActionListener(am);
      menu.add(menuItem);
      menuItem = new JMenuItem("Stop");
      menuItem.addActionListener(wl);
      menuItem.addActionListener(am);
      menu.add(menuItem);
      menuItem = new JMenuItem("Gravity");
      menuItem.addActionListener(wl);
      menu.add(menuItem);
      menuItem = new JMenuItem("Viscosity");
      menuItem.addActionListener(wl);
      menu.add(menuItem);
      subMenu = new JMenu("Simulator");
      menu.add(subMenu);
      menuItem = new JMenuItem("Delta time");
      menuItem.addActionListener(wl);
      subMenu.add(menuItem);
      menuItem = new JMenuItem("Graphics Refresh time");
      menuItem.addActionListener(wl);
      subMenu.add(menuItem);
      return mb;          
   }   
}
